clear all
cap log close
do "$dofiles/02_model\01_pre_est/readin/location_mobility_2_readin"
do "$dofiles/02_model\01_pre_est/readin/location_mobility_2_format"
gen uniqid = ER30001*1000 + ER30002 //generation of uniqid identifier variable
duplicates report uniqid //no duplicates!
drop ER30002 ER30000
tempfile loc2
save `loc2'








clear all
cap log close
do "$dofiles/02_model\01_pre_est/readin/location_mobility_readin"
do "$dofiles/02_model\01_pre_est/readin/location_mobility_format"

//generation of uniqid varaible
gen uniqid = ER30001*1000 + ER30002 //generation of uniqid identifier variable
duplicates report uniqid //no duplicates!
drop ER30002 ER30000

merge 1:1 uniqid using `loc2', keep(1 3) nogen


//loop over variables and drop what we can
ds uniqid, not
foreach var in `r(varlist)'{
	
	//fetch label
	local lab: variable label `var'
	
	//kill sequence/release/interview numbers
	if strpos("`lab'", "SEQUENCE") | strpos("`lab'", "RELEASE") | strpos("`lab'", "INTERVIEW")  {
		drop `var'
		continue
	}
	
	//educ
	if strpos("`lab'", "EDUC") | strpos("`lab'", "GRADE") | strpos("`lab'", "AGE") | strpos("`lab'", "SCHL") | strpos("`lab'", "GRAD F"){
		drop `var'
		continue
	}
}

//note: relate variable indicates end of current survey, not beginning
ds uniqid, not
foreach var in `r(varlist)'{
	noi di "ren `var' "
}
d `r(varlist)'

//note: "prior year" misleading. Means prior interview.

ren V11628 mig_1984
ren V11629 mig_mo_1984
ren V12380 state_1985
ren ER30465 relate_1985
ren V13037 mig_1985
ren V13038 mig_mo_1985
ren V13632 state_1986
ren ER30500 relate_1986
ren V14140 mig_1986
ren V14141 mig_mo_1986
ren V14679 state_1987
ren ER30537 relate_1987
ren V15148 mig_1987
ren V15149 mig_mo_1987
ren V16153 state_1988
ren ER30572 relate_1988
ren V16649 mig_1988
ren V16650 mig_mo_1988
ren V17539 state_1989
ren ER30608 relate_1989
ren V18087 mig_1989
ren V18088 mig_mo_1989
ren V18890 state_1990
ren ER30644 relate_1990
ren V19387 mig_1990
ren V19388 mig_mo_1990
ren V20190 state_1991
ren ER30691 relate_1991
ren V20687 mig_1991
ren V20688 mig_mo_1991
ren V21496 state_1992
ren ER30735 relate_1992
ren V22441 mig_1992
ren V22442 mig_mo_1992
ren V22443 mig_year_1992
ren V23328 state_1993
ren ER30808 relate_1993
ren ER2062 mig_1993
ren ER2063 mig_mo_1993
ren ER2064 mig_year_1993
ren ER4157 state_1994
ren ER33103 relate_1994
ren ER5061 mig_1994
ren ER5062 mig_mo_1994
ren ER5063 mig_year_1994
ren ER6997 state_1995
ren ER33203 relate_1995
ren ER7155 mig_1995
ren ER7156 mig_mo_1995
ren ER7157 mig_year_1995
ren ER9248 state_1996
ren ER33303 relate_1996
ren ER10004 state_1997
ren ER10072 mig_1996
ren ER10073 mig_mo_1996
ren ER10074 mig_year_1996
ren ER11842 state_gu_head_1997
ren ER33403 relate_1997
ren ER13005 state_1999
ren ER13077 mig_int_1999
ren ER13078 mig_mo_int_1999
ren ER13079 mig_year_int_1999
ren ER15922 state_gu_head_1999
ren ER33503 relate_1999
ren ER17005 state_2001
ren ER17088 mig_int_2001
ren ER17089 mig_mo_int_2001
ren ER17090 mig_year_int_2001
ren ER19983 state_gu_head_2001
ren ER33603 relate_2001
ren ER21004 state_2003
ren ER21117 mig_int_2003
ren ER21118 mig_mo_int_2003
ren ER21119 mig_year_int_2003
ren ER23420 state_gu_head_2003
ren ER33703 relate_2003
ren ER25004 state_2005
ren ER25098 mig_int_2005
ren ER25099 mig_mo_int_2005
ren ER25100 mig_year_int_2005
ren ER27386 state_gu_head_2005
ren ER33803 relate_2005
ren ER36004 state_2007
ren ER36103 mig_int_2007
ren ER36104 mig_mo_int_2007
ren ER36105 mig_year_int_2007
ren ER40561 state_gu_head_2007
ren ER33903 relate_2007
ren ER42004 state_2009
ren ER42132 mig_int_2009
ren ER42133 mig_mo_int_2009
ren ER42134 mig_year_int_2009 //2009: "where wife grew up" enters
ren ER46444 state_gu_spouse_2009
ren ER46538 state_gu_head_2009
ren ER34003 relate_2009
ren ER47304 state_2011
ren ER47440 mig_int_2011
ren ER47441 mig_mo_int_2011
ren ER47442 mig_year_int_2011
ren ER51805 state_gu_spouse_2011
ren ER51899 state_gu_head_2011
ren ER34103 relate_2011
ren ER53004 state_2013
ren ER53140 mig_int_2013
ren ER53141 mig_mo_int_2013
ren ER53142 mig_year_int_2013
ren ER57541 state_born_spouse_2013
ren ER57544 state_gu_spouse_2013
ren ER57651 state_born_head_2013
ren ER57654 state_gu_head_2013
ren ER34203 relate_2013
ren ER60004 state_2015
ren ER60155 mig_int_2015
ren ER60156 mig_mo_int_2015
ren ER60157 mig_year_int_2015
ren ER64663 state_born_spouse_2015
ren ER64666 state_gu_spouse_2015
ren ER64802 state_born_head_2015
ren ER64805 state_gu_head_2015
ren ER34303 relate_2015
ren ER66004 state_2017
ren ER66156 mig_int_2017
ren ER66157 mig_mo_int_2017
ren ER66158 mig_year_int_2017
ren ER70736 state_born_spouse_2017
ren ER70739 state_gu_spouse_2017
ren ER70874 state_born_head_2017
ren ER70877 state_gu_head_2017
ren ER34503 relate_2017

//new 2019 stuff
ren ER72004 state_2019
ren ER72156 mig_int_2019
ren ER72157 mig_mo_int_2019
ren ER72158 mig_year_int_2019
ren ER76744 state_born_spouse_2019
ren ER76747 state_gu_spouse_2019
ren ER76889 state_born_head_2019
ren ER76892 state_gu_head_2019
ren ER34703 relate_2019

        


keep uniqid state* *20* *1999




//fill in state information --first two have single digit answers
local years_1 `"1998 2000"'
foreach year in `years_1'{
	local min = `year' - 1
	local plus = `year' + 1
	gen state_`year' = .
	
	//fill in first round of missings simplistically
	replace state_`year' = state_`min' if state_`min' == state_`plus' //fill in if states before and after are the same
	replace state_`year' = state_`min' if state_`min'!=. & state_`plus' == .
	replace state_`year' = state_`plus' if state_`plus'!=. & state_`min' == .
	
	//easy filling in if move occurs
	replace state_`year' = state_`plus' if mig_year_int_`plus' == 1 //switch to later year if move happened in first year
	replace state_`year' = state_`min' if mig_year_int_`plus' == 3 //switch to later year if move happened in first year
	
	//more difficult
	replace state_`year' = state_`min' if mig_year_int_`plus' == 2 & mig_mo_int_`plus'>6 & mig_mo_int_`plus'<13 & state_`year' ==. //switch to earlier year if move happend in latter half of middle year
	replace state_`year' = state_`plus' if mig_year_int_`plus' == 2 & mig_mo_int_`plus'<6 & mig_mo_int_`plus'>0 & state_`year' ==. //switch to earlier year if move happend in earlier half of middle year
	
	//final punt: fill in with first year if otherwise indeterminant
	replace state_`year' = state_`min' if state_`year' == . & state_`min' !=. & state_`plus' !=.
	assert state_`year'!=. if state_`min'!=. & state_`plus'!=.
}

//sanity check yields a reasonable yearly cross-state migration rate. hooray!
count if state_1998 != state_1999 & state_1999 !=. & state_1998!=.
count if state_1998!=. & state_1999!=. 


//now fill in the remainder
local years_2 = `"2002 2004 2006 2008 2010 2012 2014 2016 2018"'
foreach year in `years_2'{
	local min = `year' - 1
	local plus = `year' + 1
	gen state_`year' = .
	
	//fill in first round of missings simplistically
	replace state_`year' = state_`min' if state_`min' == state_`plus' //fill in if states before and after are the same
	replace state_`year' = state_`min' if state_`min'!=. & state_`plus' == .
	replace state_`year' = state_`plus' if state_`plus'!=. & state_`min' == .
	
	//easy filling in if move occurs
	replace state_`year' = state_`plus' if mig_year_int_`plus' == `min' //switch to later year if move happened in first year
	replace state_`year' = state_`min' if mig_year_int_`plus' == `plus' //switch to later year if move happened in first year
	
	//more difficult
	replace state_`year' = state_`min' if mig_year_int_`plus' == `year' & mig_mo_int_`plus'>6 & mig_mo_int_`plus'<13 & state_`year' ==. //switch to earlier year if move happend in latter half of middle year
	replace state_`year' = state_`plus' if mig_year_int_`plus' == `year' & mig_mo_int_`plus'<6 & mig_mo_int_`plus'>0 & state_`year' ==. //switch to earlier year if move happend in earlier half of middle year
	
	//final punt: fill in with first year if otherwise indeterminant
	replace state_`year' = state_`min' if state_`year' == . & state_`min' !=. & state_`plus' !=.
	assert state_`year'!=. if state_`min'!=. & state_`plus'!=.
}

//sanity check yields a reasonable yearly cross-state migration rate. hooray!
count if state_2004 != state_2005 & state_2004 !=. & state_2005!=.
count if state_2004!=. & state_2005!=. 
drop mig* //nix migration variables

//goal: complete history of state for each individual from 1986 to current, and then additinoally store birth/grew up locations for adults
reshape long state_ relate_ state_born_head_ state_gu_head_ state_born_spouse_ state_gu_spouse_, i(uniqid) j(year)
	
//start cleaning up
*****restrict to a few values of relate variable and standardize
replace relate = 1 if relate == 10
replace relate = 2 if relate == 20
replace relate = 3 if relate == 30
keep if relate == 1 | relate == 2 | relate == 3 | relate == .

tab year

//code birth state/state where raised
gen state_born = .
replace state_born = state_born_head if relate == 1
replace state_born = state_born_spouse if relate == 2

gen state_raised = .
replace state_raised = state_gu_head if relate == 1
replace state_raised = state_gu_spouse if relate == 2
drop state_born_head state_born_spouse state_gu* relate

//kill if never observed
ren state_ state
bys uniqid: egen temp = max(state)
drop if temp == .
order uniqid year state state_raised state_born

//small cleanup
drop temp
gen temp = state
drop state 
ren temp state

//final step: fill in state raised and born info for missing observations
//use FIRST response, as people aren't perfectly consistent over time
sort uniqid year
replace state_raised = state_raised[_n-1] if state_raised[_n-1]!=. & uniqid[_n] == uniqid[_n-1] //fill in downward	
replace state_born = state_born[_n-1] if state_born[_n-1]!=. & uniqid[_n] == uniqid[_n-1] //fill in downward	

count
forval i = 1/`r(N)'{
	local num = `r(N)' - `i' + 1
	
	replace state_raised = state_raised[_n+1] if state_raised[_n+1]!=. & uniqid[_n] == uniqid[_n+1] in `num' //fill in upward	
	replace state_born = state_born[_n+1] if state_born[_n+1]!=. & uniqid[_n] == uniqid[_n+1] in `num' //fill in upward	
}

//check to make sure that this worked
compress
save "$temp/psid_location_mobility", replace	
	
	
	
//end of do-file